namespace QMVnSoft.DAL.Util
{    
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Windows.Forms;      

    public class FormCreateActiveCode : Form
    {
        private Button btnActiveCode;
        private Button btnCancel;
        private Button btnOk;
        private Button btnPathBackup;
        private IContainer components;
        private Label lblNgay;
        private Label lblPathBackup;
        private Label lblSo_Luong;
        private DateTimePicker txtNgay;
        private TextBox txtPathBackup;
        private TextBox txtSo_Luong;

        public FormCreateActiveCode()
        {
            this.InitializeComponent();
            //this.txtNgay.Value = DateTime.Today;
            this.txtSo_Luong.Text = "50";
        }

        private void btnActiveCode_Click(object sender, EventArgs e)
        {
            if (this.txtNgay.Value.Equals(DBNull.Value) || (this.txtSo_Luong.Text == "0"))
            {
                MessageBox.Show("Chưa đưa đủ Thông tin ngày tháng và số lượng!", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                DataTable dataTable = new DataTable("CardNumber");
                DataRow row = null;
                DataColumn column = new DataColumn();
                column.ColumnName = "CardNumber";
                column.DataType = System.Type.GetType("System.String");
                column.MaxLength = 0x18;
                dataTable.Columns.Add(column);
                dataTable.PrimaryKey = new DataColumn[] { column };
                string key = "";
                for (int i = 0; i < int.Parse( this.txtSo_Luong.Text); i++)
                {
                    key = AppSecurity.SoftwareEncode(0x89);
                    while (dataTable.Rows.Find(key) != null)
                    {
                        key = AppSecurity.SoftwareEncode(0x89);
                    }
                    row = dataTable.NewRow();
                    row["CardNumber"] = key;
                    dataTable.Rows.Add(row);
                }
                //StiReport report = new StiReport();
                //report.DataStore.Clear();
                //report.RegData("CardNumber", dataTable);
                //report.Load(@"ReportFiles\CardNumber.mrt");
                //report.Show();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.txtNgay.Value.Equals(DBNull.Value) || (this.txtSo_Luong.Text == "0"))
            {
                MessageBox.Show("Chưa đưa đủ thông tin ngày tháng và số lượng!", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                DataTable table = new DataTable("CardNumber");
                DataRow row = null;
                DataColumn column = new DataColumn();
                column.ColumnName = "CardNumber";
                column.DataType = System.Type.GetType("System.String");
                column.MaxLength = 0x18;
                DataColumn column2 = new DataColumn();
                column2.ColumnName = "CreatedDate";
                column2.DataType = System.Type.GetType("System.DateTime");
                DataColumn column3 = new DataColumn();
                column3.ColumnName = "TaxCode";
                column3.DataType = System.Type.GetType("System.String");
                column3.MaxLength = 128;
                DataColumn column4 = new DataColumn();
                column4.ColumnName = "LocalName";
                column4.DataType = System.Type.GetType("System.String");
                column4.MaxLength = 128;
                DataColumn column5 = new DataColumn();
                column5.ColumnName = "EnglishName";
                column5.DataType = System.Type.GetType("System.String");
                column5.MaxLength = 128;
                DataColumn column6 = new DataColumn();
                column6.ColumnName = "LocalAddress";
                column6.DataType = System.Type.GetType("System.String");
                column6.MaxLength = 128;
                DataColumn column7 = new DataColumn();
                column7.ColumnName = "EnglishAddress";
                column7.DataType = System.Type.GetType("System.String");
                column7.MaxLength = 128;
                DataColumn column8 = new DataColumn();
                column8.ColumnName = "RegisteredFile";
                column8.DataType = System.Type.GetType("System.String");
                table.Columns.Add(column);
                table.Columns.Add(column2);
                table.Columns.Add(column3);
                table.Columns.Add(column4);
                table.Columns.Add(column5);
                table.Columns.Add(column6);
                table.Columns.Add(column7);
                table.Columns.Add(column8);
                table.PrimaryKey = new DataColumn[] { column };
                DataTable dataTable = new DataTable("NewCardNumber");
                DataRow row2 = null;
                DataColumn column9 = new DataColumn();
                column9.ColumnName = "CardNumber";
                column9.DataType = System.Type.GetType("System.String");
                column9.MaxLength = 0x18;
                dataTable.Columns.Add(column9);
                if (File.Exists(this.txtPathBackup.Text + @"\CardNumber.xml"))
                {
                    table.ReadXml(this.txtPathBackup.Text + @"\CardNumber.xml");
                }
                string key = "";
                for (int i = 0; i < int.Parse( this.txtSo_Luong.Text); i++)
                {
                    key = AppSecurity.SoftwareEncode(0x8b);
                    while (table.Rows.Find(key) != null)
                    {
                        key = AppSecurity.SoftwareEncode(0x8b);
                    }
                    row = table.NewRow();
                    row["CreatedDate"] = this.txtNgay.Value;
                    row["CardNumber"] = key;
                    table.Rows.Add(row);
                    row2 = dataTable.NewRow();
                    row2["CardNumber"] = key;
                    dataTable.Rows.Add(row2);
                }
                //StiReport report = new StiReport();
                //report.DataStore.Clear();
                //report.RegData("CardNumber", dataTable);
                //report.Load(@"ReportFiles\CardNumber.mrt");
                //report.Show();
                if (MessageBox.Show("Bạn có muốn lưu danh s\x00e1ch mã số này không?", "Thong bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    table.WriteXml(this.txtPathBackup.Text + @"\CardNumber.xml");
                }
            }
        }

        private void btnPathBackup_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.ShowNewFolderButton = true;
            dialog.ShowDialog();
            this.txtPathBackup.Text = dialog.SelectedPath;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.txtNgay = new System.Windows.Forms.DateTimePicker();
            this.lblNgay = new System.Windows.Forms.Label();
            this.txtSo_Luong = new System.Windows.Forms.TextBox();
            this.lblSo_Luong = new System.Windows.Forms.Label();
            this.btnOk = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.btnPathBackup = new System.Windows.Forms.Button();
            this.lblPathBackup = new System.Windows.Forms.Label();
            this.txtPathBackup = new System.Windows.Forms.TextBox();
            this.btnActiveCode = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // txtNgay
            // 
            this.txtNgay.Font = new System.Drawing.Font("Arial", 8.25F);
            this.txtNgay.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            this.txtNgay.Location = new System.Drawing.Point(109, 12);
            this.txtNgay.Name = "txtNgay";
            this.txtNgay.Size = new System.Drawing.Size(100, 20);
            this.txtNgay.TabIndex = 0;
            // 
            // lblNgay
            // 
            this.lblNgay.AutoSize = true;
            this.lblNgay.Font = new System.Drawing.Font("Arial", 8.25F);
            this.lblNgay.Location = new System.Drawing.Point(12, 15);
            this.lblNgay.Name = "lblNgay";
            this.lblNgay.Size = new System.Drawing.Size(49, 14);
            this.lblNgay.TabIndex = 1;
            this.lblNgay.Text = "Ngày lập";
            // 
            // txtSo_Luong
            // 
            this.txtSo_Luong.Font = new System.Drawing.Font("Arial", 8.25F);
            this.txtSo_Luong.Location = new System.Drawing.Point(109, 38);
            this.txtSo_Luong.Name = "txtSo_Luong";
            this.txtSo_Luong.Size = new System.Drawing.Size(100, 20);
            this.txtSo_Luong.TabIndex = 1;
            this.txtSo_Luong.Text = "0";
            this.txtSo_Luong.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // lblSo_Luong
            // 
            this.lblSo_Luong.AutoSize = true;
            this.lblSo_Luong.Font = new System.Drawing.Font("Arial", 8.25F);
            this.lblSo_Luong.Location = new System.Drawing.Point(12, 41);
            this.lblSo_Luong.Name = "lblSo_Luong";
            this.lblSo_Luong.Size = new System.Drawing.Size(51, 14);
            this.lblSo_Luong.TabIndex = 1;
            this.lblSo_Luong.Text = "Số lượng";
            // 
            // btnOk
            // 
            this.btnOk.Font = new System.Drawing.Font("Arial", 8.25F);
            this.btnOk.Location = new System.Drawing.Point(245, 130);
            this.btnOk.Name = "btnOk";
            this.btnOk.Size = new System.Drawing.Size(112, 23);
            this.btnOk.TabIndex = 5;
            this.btnOk.Text = "&Lập mã thẻ";
            this.btnOk.UseVisualStyleBackColor = true;
            this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
            // 
            // btnCancel
            // 
            this.btnCancel.Font = new System.Drawing.Font("Arial", 8.25F);
            this.btnCancel.Location = new System.Drawing.Point(363, 130);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(112, 23);
            this.btnCancel.TabIndex = 6;
            this.btnCancel.Text = "&Huỷ bỏ";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // btnPathBackup
            // 
            this.btnPathBackup.Font = new System.Drawing.Font("Arial", 8.25F);
            this.btnPathBackup.Location = new System.Drawing.Point(446, 63);
            this.btnPathBackup.Name = "btnPathBackup";
            this.btnPathBackup.Size = new System.Drawing.Size(29, 23);
            this.btnPathBackup.TabIndex = 3;
            this.btnPathBackup.TabStop = false;
            this.btnPathBackup.Text = "...";
            this.btnPathBackup.UseVisualStyleBackColor = true;
            this.btnPathBackup.Click += new System.EventHandler(this.btnPathBackup_Click);
            // 
            // lblPathBackup
            // 
            this.lblPathBackup.AutoSize = true;
            this.lblPathBackup.Font = new System.Drawing.Font("Arial", 8.25F);
            this.lblPathBackup.Location = new System.Drawing.Point(12, 67);
            this.lblPathBackup.Name = "lblPathBackup";
            this.lblPathBackup.Size = new System.Drawing.Size(62, 14);
            this.lblPathBackup.TabIndex = 6;
            this.lblPathBackup.Text = "Đường dẫn";
            // 
            // txtPathBackup
            // 
            this.txtPathBackup.Font = new System.Drawing.Font("Arial", 8.25F);
            this.txtPathBackup.Location = new System.Drawing.Point(109, 64);
            this.txtPathBackup.Name = "txtPathBackup";
            this.txtPathBackup.Size = new System.Drawing.Size(331, 20);
            this.txtPathBackup.TabIndex = 2;
            // 
            // btnActiveCode
            // 
            this.btnActiveCode.Font = new System.Drawing.Font("Arial", 8.25F);
            this.btnActiveCode.Location = new System.Drawing.Point(127, 130);
            this.btnActiveCode.Name = "btnActiveCode";
            this.btnActiveCode.Size = new System.Drawing.Size(112, 23);
            this.btnActiveCode.TabIndex = 4;
            this.btnActiveCode.Text = "Lập mã &kích hoạt";
            this.btnActiveCode.UseVisualStyleBackColor = true;
            this.btnActiveCode.Click += new System.EventHandler(this.btnActiveCode_Click);
            // 
            // FormCreateActiveCode
            // 
            this.ClientSize = new System.Drawing.Size(487, 165);
            this.Controls.Add(this.btnPathBackup);
            this.Controls.Add(this.lblPathBackup);
            this.Controls.Add(this.txtPathBackup);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.btnActiveCode);
            this.Controls.Add(this.btnOk);
            this.Controls.Add(this.txtSo_Luong);
            this.Controls.Add(this.lblSo_Luong);
            this.Controls.Add(this.lblNgay);
            this.Controls.Add(this.txtNgay);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "FormCreateActiveCode";
            this.Text = "Lập mã thẻ khách hàng";
            this.ResumeLayout(false);
            this.PerformLayout();

        }
    }
}

